<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="仓库名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入仓库名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="是否启用" prop="enable_flag">
        <el-input v-model="queryParams.enable_flag" placeholder="请输入是否启用" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="更新时间">
        <el-date-picker v-model="daterangeUpdate_time" style="width: 240px" value-format="yyyy-MM-dd" type="daterange"
          range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd({id:0})"
          v-hasPermi="['wms:warehouse:add']">新增顶级仓库</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="info" plain icon="el-icon-sort" size="mini" @click="toggleExpandAll">展开/折叠</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-if="refreshTable" v-loading="loading" :data="warehouseList" row-key="id"
      :default-expand-all="isExpandAll" :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
      <el-table-column label="库位上级" prop="parent_id" />
      <!-- <el-table-column label="仓库祖籍" align="center" prop="ancestors" /> -->
      <el-table-column label="仓库名称" align="center" prop="name" />
      <!-- <el-table-column label="位置" align="center" prop="location" /> -->
      <el-table-column label="面积" align="center" prop="area" />
      <el-table-column label="负责人" align="center" prop="user_id" />
      <!-- <el-table-column label="存放物品列表" align="center" prop="goods_list" /> -->
      <el-table-column label="是否启用" align="center" key="enable_flag">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.enable_flag"
            active-value="0"
            inactive-value="1"
            @change="handleEnableChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column width="150" label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['wms:warehouse:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['wms:warehouse:remove']">删除</el-button>
          <el-button size="mini" type="text" icon="el-icon-plus" @click="handleAdd(scope.row)"
            v-hasPermi="['wms:warehouse:add']">新增库位</el-button>
        </template>
      </el-table-column>
<!--      <el-table-column label="位置x" align="center" prop="position_x" />
      <el-table-column label="位置y" align="center" prop="position_y" />
      <el-table-column label="位置z" align="center" prop="position_z" /> -->
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="创建者" align="center" prop="create_by" />
      <el-table-column label="创建时间" align="center" prop="create_time" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.create_time, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新者" align="center" prop="update_by" />
      <el-table-column label="更新时间" align="center" prop="update_time" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.update_time, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改仓库配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="库位上级" prop="parent_id">
              <el-input v-model="form.parent_id" placeholder="请输入上级仓库" />
            </el-form-item>
            <el-form-item label="名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入仓库名称" />
            </el-form-item>
            <el-form-item label="位置" prop="location">
              <el-input v-model="form.location" type="textarea" placeholder="请输入内容" />
            </el-form-item>
            <el-form-item label="面积" prop="area">
              <el-input v-model="form.area" placeholder="请输入面积" />
            </el-form-item>
            <el-form-item label="负责人" prop="user_id">
              <el-input v-model="form.user_id" placeholder="请输入负责人" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否启用">
              <el-radio-group v-model="form.enable_flag">
                <el-radio v-for="dict in dict.type.sys_normal_disable" :key="dict.value"
                  :label="dict.value">{{dict.label}}</el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="位置x" prop="position_x">
              <el-input v-model="form.position_x" placeholder="请输入位置x" />
            </el-form-item>
            <el-form-item label="位置y" prop="position_y">
              <el-input v-model="form.position_y" placeholder="请输入位置y" />
            </el-form-item>
            <el-form-item label="位置z" prop="position_z">
              <el-input v-model="form.position_z" placeholder="请输入位置z" />
            </el-form-item>
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listWarehouse,
    getWarehouse,
    delWarehouse,
    addWarehouse,
    updateWarehouse
  } from "@/api/wms/warehouse";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

  export default {
    name: "Warehouse",
    dicts: ['sys_normal_disable'],
    components: {
      Treeselect
    },
    data() {
      return {
        // 遮罩层
        loading: true,
        // 显示搜索条件
        showSearch: true,
        // 仓库配置表格数据
        warehouseList: [],
        // 仓库配置树选项
        warehouseOptions: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 是否展开，默认全部展开
        isExpandAll: true,
        // 重新渲染表格状态
        refreshTable: true,
        // $comment时间范围
        daterangeUpdate_time: [],
        // 查询参数
        queryParams: {
          name: null,
          enable_flag: null,
          update_time: null,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          name: [{
            required: true,
            message: "仓库名称不能为空",
            trigger: "blur"
          }],
        }
      };
    },
    created() {
      this.getList();
    },
    methods: {
      /** 查询仓库配置列表 */
      getList() {
        this.loading = true;

        if (null != this.daterangeUpdate_time && '' != this.daterangeUpdate_time) {
          this.queryParams.beginUpdate_time = this.daterangeUpdate_time[0];
          this.queryParams.endUpdate_time = this.daterangeUpdate_time[1];
        }
        listWarehouse(this.queryParams).then(response => {
          this.warehouseList = this.handleTree(response.data, "id", "parent_id");
          this.loading = false;
        });
      },
      /** 转换仓库配置数据结构 */
      normalizer(node) {
        if (node.children && !node.children.length) {
          delete node.children;
        }
        return {
          id: node.id,
          label: node.name,
          children: node.children
        };
      },
      /** 查询仓库配置下拉树结构 */
      getTreeselect() {
        listWarehouse().then(response => {
          this.warehouseOptions = [];
          const data = {
            parent_id: 0,
            name: '顶级节点',
            children: []
          };
          data.children = this.handleTree(response.data, "id", "parent_id");
          this.warehouseOptions.push(data);
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          parent_id: null,
          ancestors: null,
          name: null,
          location: null,
          area: null,
          user_id: null,
          goods_list: null,
          enable_flag: null,
          position_x: null,
          position_y: null,
          position_z: null,
          remark: null,
          create_by: null,
          create_time: null,
          update_by: null,
          update_time: null,
          tenant_id: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.daterangeUpdate_time = [];
        this.resetForm("queryForm");
        this.handleQuery();
      },
      /** 新增按钮操作 */
      handleAdd(row) {
        this.reset();
        debugger;
        // this.getTreeselect();
        if (row != null && row.id) {
          this.form.parent_id = row.id;
        } else {
          this.form.parent_id = 0;
        }
        this.open = true;
        this.title = "添加仓库配置";
        this.form.enable_flag = "0";
      },
      /** 展开/折叠操作 */
      toggleExpandAll() {
        this.refreshTable = false;
        this.isExpandAll = !this.isExpandAll;
        this.$nextTick(() => {
          this.refreshTable = true;
        });
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        this.getTreeselect();
        if (row != null) {
          this.form.parent_id = row.id;
        }
        getWarehouse(row.id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改仓库配置";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateWarehouse(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addWarehouse(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        this.$modal.confirm('是否确认删除仓库配置编号为"' + row.id + '"的数据项？').then(function() {
          return delWarehouse(row.id);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      handleEnableChange(row){
        let text = row.enable_flag === "0" ? "启用" : "停用";
        this.$modal.confirm('确认要"' + text + '""' + row.userName + '"用户吗？').then(function () {
          // return changeUserStatus(row.id, row.enable_flag);
          return null;
        }).then(() => {
          this.$modal.msgSuccess(text + "成功");
        }).catch(function () {
          row.enable_flag = row.enable_flag === "0" ? "1" : "0";
        });
      },
    }
  };
</script>
